﻿#include "productcountstandarditemmodel.h"
#include "tools.h"

ProductCountStandardItemModel::ProductCountStandardItemModel(QObject *parent) : QStandardItemModel(parent)
{
    header<<"商品ID"
         <<"型号"
        <<"型号拼音码"
       <<"名称"
      <<"名称拼音码"
     <<"单位"
    <<"下限"
    <<"上限"
    <<"库存数量"
    <<"三包数量"
    <<"净价"
    <<"盘点数量"
    <<"盘赢盘亏数量"
    <<"盘赢盘亏金额"
    <<"货号"
    <<"品牌"
    <<"产地"
    <<"规格"
    <<"条型码"
    <<"备注"
    <<"指导价格1"
    <<"指导价格2"
    <<"一级类别"
    <<"二级类别";

    countIndex = 12;
    moneyIndex = 13;

    setHorizontalHeaderLabels(header);
}

bool ProductCountStandardItemModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(index.column() != 6 && index.column()!=7 && index.column()!=11){
        return false;
    }

    Product *product = products[index.row()];

    int lowerLimit;
    int upperLimit;
    int countNum; //盘点数量
    if(index.column() == 6){
        lowerLimit = value.toInt();
        if(lowerLimit <= product->upperLimit){
            product->lowerLimit = lowerLimit;
            QStandardItemModel::setData(index, lowerLimit, role);
        }
    }else if(index.column() == 7){
        upperLimit = value.toInt();
        if(upperLimit >= product->lowerLimit){
            product->upperLimit = upperLimit;
            QStandardItemModel::setData(index, upperLimit, role);
        }
    }else{
        countNum = value.toInt();
        if(countNum<0) countNum=0;
        product->countNum = countNum;
        QStandardItemModel::setData(index, countNum, role);
        QStandardItemModel::setItem(index.row(), 12, new QStandardItem(QString("%1").arg(countNum-product->count-product->sanbao)));
        QStandardItemModel::setItem(index.row(), 13, new QStandardItem(QString("%1").arg((countNum-product->count-product->sanbao)*product->inPrice)));
    }

    refreshTotalValue();

    return true;
}

QVariant ProductCountStandardItemModel::data(const QModelIndex &index, int role) const{
    QVariant value = QStandardItemModel::data(index, role);
    //设置内容居中
    if(role==Qt::TextAlignmentRole){
        return  Qt::AlignCenter;
    }

    //设置内容颜色
    if (role == Qt::TextColorRole && (index.column() == 6 || index.column() == 7 || index.column() == 11))
        return QVariant::fromValue(QColor(Qt::blue));
    return value;
}

void ProductCountStandardItemModel::reset(){
    clear();
    products.clear();
    setHorizontalHeaderLabels(header);
}

QList<Product*> ProductCountStandardItemModel::currentProducts(){
    return products;
}

void ProductCountStandardItemModel::insertProduct(Product &product, int defaultCount){
    QList<QStandardItem*> rowData;

    Q_UNUSED(defaultCount)
    product.countNum = product.count+product.sanbao;
    rowData.append(new QStandardItem(product.productId));
    rowData.append(new QStandardItem(product.type));
    rowData.append(new QStandardItem(product.typePinyin));
    rowData.append(new QStandardItem(product.name));
    rowData.append(new QStandardItem(product.namePinyin));
    rowData.append(new QStandardItem(product.unit));
    rowData.append(new QStandardItem(QString("%1").arg(product.lowerLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.upperLimit)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count)));
    rowData.append(new QStandardItem(QString("%1").arg(product.sanbao)));
    rowData.append(new QStandardItem(QString::number(product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(QString("%1").arg(product.countNum)));
    rowData.append(new QStandardItem(QString("%1").arg(product.count+product.sanbao-product.countNum)));
    rowData.append(new QStandardItem(QString::number((product.count+product.sanbao-product.countNum)*product.inPrice, 'f', 2)));
    rowData.append(new QStandardItem(product.productNum));
    rowData.append(new QStandardItem(product.brand));
    rowData.append(new QStandardItem(product.originPlace));
    rowData.append(new QStandardItem(product.specifications));
    rowData.append(new QStandardItem(product.barCode));
    rowData.append(new QStandardItem(product.remark));
    rowData.append(new QStandardItem(QString::number(product.guidePrice1, 'f', 2)));
    rowData.append(new QStandardItem(QString::number(product.guidePrice2, 'f', 2)));
    rowData.append(new QStandardItem(product.primaryDirectory));
    rowData.append(new QStandardItem(product.secondaryDirectory));

    products.append(&product);
    insertRow(rowCount()-1, rowData);
}

void ProductCountStandardItemModel::insertProduct(QList<Product*> products, int defaultCount){
    int i=0;

    for(Product *product:products){
        if(product->checkExists(this->products)){
            i++;
        }else{
            insertProduct(*product, defaultCount);
        }
    }

    if(i!=0){
        Tools::showRepeatProductMsg();
    }

    refreshTotalValue();
}

void ProductCountStandardItemModel::refreshTotalValue(){
    int totalCount = 0;
    double totalMoney = 0.0;

    for(int i=0; i<rowCount(); i++){
        QStandardItem *curCountItem = item(i, countIndex);
        QStandardItem *curMoneyItem = item(i, moneyIndex);
        if(curCountItem == nullptr || curMoneyItem == nullptr){
            break;
        }
        totalCount += curCountItem->text().toInt();
        totalMoney += curMoneyItem->text().toDouble();
    }

    emit updateTotalValue(totalCount, totalMoney);
}
